/**
 * 电影、音乐、句子路由
 */

const Router = require('koa-router')
const router = new Router()
router.prefix('/api/classic')
const { ClassicValidator, ArtIndexValidator } = require('@validator/validator.js')
const { Art } = require('@model/art.js')
const { SuccessModel } = require('@model/resModel.js')
const { Flow } = require('@model/flow.js')
const { Auth } = require('@middlewares/auth.js')

router.get('/detail/:artId/:type', async (ctx, next) => {
  const v = await new ClassicValidator().validate(ctx)
  const artId = v.get('path.artId')
  const type = v.get('path.type')
  const art = await Art.getDetail(artId, type)
  const request = `${ctx.method} ${ctx.path}`
  ctx.body = new SuccessModel(art, request)
})

router.get('/latest', async (ctx, next) => {
  const art = await Flow.getLatestArt()
  const request = `${ctx.method} ${ctx.path}`
  ctx.body = new SuccessModel(art, request)
})

router.get('/likestatus/:artId/:type', new Auth().controllerAuthApi, async (ctx, next) => {
  const v = await new ClassicValidator().validate(ctx)
  const artId = v.get('path.artId')
  const type = v.get('path.type')
  const artWithme = await Art.artLikeStatus(artId, type, ctx.auth.uid)
  const request = `${ctx.method} ${ctx.path}`
  // 获取作品点赞状态，以及点赞数量
  ctx.body = new SuccessModel(artWithme, request)
})

router.get('/list/:type', async (ctx, next) => {
  const type = ctx.params.type
  const arts = await Art.getList(type)
  // const request = `${ctx.method} ${ctx.path}`
  ctx.body = new SuccessModel(arts, ctx.requestApi)
})

// 获取上一期
router.get('/previous/:index', async (ctx, next) => {
  const v = await new ArtIndexValidator().validate(ctx)
  const index = v.get('path.index')
  const art = await Art.getPrevious(index)
  ctx.body = new SuccessModel(art, ctx.requestApi)
})

// 获取下一期
router.get('/next/:index', async (ctx, next) => {
  const v = await new ArtIndexValidator().validate(ctx)
  const index = v.get('path.index')
  const art = await Art.getNext(index)
  ctx.body = new SuccessModel(art, ctx.requestApi)
})

module.exports = router